/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.free.ts.infrastructure.web;

import be.free.ts.activity.web.ActivityRequest;
import be.free.ts.contact.web.ContactRequest;
import be.free.ts.customer.web.CustomerRequest;
import be.free.ts.employee.web.EmployeeRequest;
import be.free.ts.project.web.ProjectRequest;
import be.free.ts.support.web.ErrorDialog;
import be.free.ts.timesheet.web.TimesheetRequest;
import com.google.common.collect.Lists;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryLogHandler;
import com.google.web.bindery.requestfactory.shared.LoggingRequest;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Christophe De Blende
 * @since 03/01/13
 */
public class RequestFactoryGinModule extends AbstractGinModule {

	@Override
	protected void configure() {
		bind(EventBus.class).to(SimpleEventBus.class);

	}

	@Provides
	@Singleton
	public TimesheetRequestFactory timesheetRequestFactory(EventBus eventBus) {
		final TimesheetRequestFactory factory = GWT.create(TimesheetRequestFactory.class);
		factory.initialize(eventBus);
		// Add remote logging handler
		RequestFactoryLogHandler.LoggingRequestProvider provider = new RequestFactoryLogHandler.LoggingRequestProvider() {
			public LoggingRequest getLoggingRequest() {
				return factory.loggingRequest();
			}
		};
		Logger.getLogger("").addHandler(new ErrorDialog().getHandler());
		Logger.getLogger("").addHandler(new RequestFactoryLogHandler(provider, Level.WARNING, Lists.<String>newArrayList()));
		return factory;
	}

	@Provides
	public ActivityRequest activityRequest(TimesheetRequestFactory factory) {
		return factory.activityRequest();
	}

	@Provides
	public ContactRequest contactRequest(TimesheetRequestFactory factory) {
		return factory.contactRequest();
	}

	@Provides
	public CustomerRequest customerRequest(TimesheetRequestFactory factory) {
		return factory.customerRequest();
	}

	@Provides
	public EmployeeRequest employeeRequest(TimesheetRequestFactory factory) {
		return factory.employeeRequest();
	}

	@Provides
	public ProjectRequest projectRequest(TimesheetRequestFactory factory) {
		return factory.projectRequest();
	}

	@Provides
	public TimesheetRequest timesheetRequest(TimesheetRequestFactory factory) {
		return factory.timesheetRequest();
	}
}
